﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FarPoint.Web.Spread;
using FarPoint.Web.Spread.Model;
using FarPoint.CalcEngine;
using System.Data;
using System.Drawing;
using System.IO;

namespace ControlExplorer
{
    public partial class Template_Edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 初始化模板

                string tid = "";
                tid = Request.QueryString["tid"];
                hfTemplateID.Value = tid;

                DataTable dt = new DataTable();
                string commandstring = string.Format("SELECT * FROM 模板列表 WHERE 模板编号='{0}'", tid);
                string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;

                using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionstring))
                {
                    System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(commandstring, connection);
                    System.Data.OleDb.OleDbCommandBuilder builder = new System.Data.OleDb.OleDbCommandBuilder(adapter);
                    adapter.Fill(dt);
                }

                byte[] templatebytes = dt.Rows[0]["模板"] as byte[];
                System.IO.MemoryStream ms = new System.IO.MemoryStream(templatebytes);

                SheetSkin skin = fpTemplate.Sheets[0].ActiveSkin;

                fpTemplate.Open(ms);
                fpTemplate.EnableAjaxCall = true;
                fpTemplate.EnableClientScript = true;
                fpTemplate.Sheets[0].AllowPage = false;
                fpTemplate.CommandBar.Visible = false;
                fpTemplate.Sheets[0].ActiveSkin = skin;
                fpTemplate.UseClipboard = false;
                fpTemplate.Sheets[0].Protect = true;
                fpTemplate.Sheets[0].LockBackColor = Color.LightGray;


                #endregion

                #region 填充模板数据

                List<string> columns = DataTools.GetTemplateDataFields(hfTemplateID.Value);

                // 所有存放数据的表结构中，第一列为ID、第二列为记录编号，所以i从2开始
                for (int i = 1; i < columns.Count; i++)
                {
                    ExternalCellExpression expr = fpTemplate.Sheets[0].GetCustomName(columns[i]) as ExternalCellExpression;
                    if (expr != null)
                    {
                        fpTemplate.Sheets[0].SetText(expr.Row, expr.Column, "[" + columns[i] + "]");
                    }
                    lstDataFields.Items.Add(columns[i]);
                }

                DataRow dr = DataTools.GetTemplate(hfTemplateID.Value);

                if (dr["模板类型"].Equals("动态行模板"))
                {
                    ExternalRangeExpression expr = fpTemplate.Sheets[0].GetCustomName("DataArea") as ExternalRangeExpression;
                    if (expr == null)
                    {
                        Response.Write("<script>alert('该模板还没有设置数据绑定区域，请通过\"设置数据区域\"按钮进行设置！');</script>");
                    }
                }

                #endregion
            }
        }

        #region 菜单操作

        // 保存模板
        private void SaveTemplate()
        {
            fpTemplate.SaveChanges();

            string tid = hfTemplateID.Value;
            UpdataTemplate(tid);

        }

        // 保存上传的模板至数据库
        private void UpdataTemplate(string tid)
        {
            fpTemplate.Sheets[0].LockBackColor = Color.Transparent;

            List<string> columns = DataTools.GetTemplateDataFields(tid);

            // 清除数据字段对应单元格的文本
            for (int i = 0; i < columns.Count; i++)
            {
                ExternalCellExpression expr = fpTemplate.Sheets[0].GetCustomName(columns[i]) as ExternalCellExpression;
                if (expr != null)
                {
                    fpTemplate.Sheets[0].SetText(expr.Row, expr.Column, null);
                }
            }

            // 插入模板
            string commandstring = "SELECT * FROM 模板列表 WHERE 模板编号='" + tid + "'";
            string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;

            using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionstring))
            {
                System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(commandstring, connection);
                System.Data.OleDb.OleDbCommandBuilder builder = new System.Data.OleDb.OleDbCommandBuilder(adapter);

                DataTable dt = new DataTable();
                adapter.Fill(dt);
                DataRow dr = dt.Rows[0];

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                fpTemplate.Save(ms, false);

                byte[] templatebytes = new byte[ms.Length];
                templatebytes = ms.ToArray();
                ms.Close();

                dr["模板"] = templatebytes;
                adapter.Update(dt);
            }

            fpTemplate.Sheets[0].LockBackColor = Color.LightGray;

            // 恢复数据字段对应单元格的文本
            for (int i = 0; i < columns.Count; i++)
            {
                ExternalCellExpression expr = fpTemplate.Sheets[0].GetCustomName(columns[i]) as ExternalCellExpression;
                if (expr != null)
                {
                    fpTemplate.Sheets[0].SetText(expr.Row, expr.Column, string.Format("[{0}]", columns[i]));
                }
            }
        }

        // 加粗
        private void FontBold()
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;
            StyleInfo oldstyle = iss.GetCompositeInfo(r, c, 0, null);

            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].SelectionModel[0];

            for (int i = 0; i < cr.RowCount; i++)
            {
                for (int j = 0; j < cr.ColumnCount; j++)
                {
                    StyleInfo newstyle = iss.GetCompositeInfo(i + cr.Row, j + cr.Column, 0, null);
                    newstyle.Font.Bold = !oldstyle.Font.Bold;
                    iss.SetDirectInfo(i + cr.Row, j + cr.Column, newstyle);
                }
            }
        }

        // 斜体
        private void FontItalic()
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;
            StyleInfo oldstyle = iss.GetCompositeInfo(r, c, 0, null);

            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].SelectionModel[0];

            for (int i = 0; i < cr.RowCount; i++)
            {
                for (int j = 0; j < cr.ColumnCount; j++)
                {
                    StyleInfo newstyle = iss.GetCompositeInfo(i + cr.Row, j + cr.Column, 0, null);
                    newstyle.Font.Italic = !oldstyle.Font.Italic;
                    iss.SetDirectInfo(i + cr.Row, j + cr.Column, newstyle);
                }
            }
        }

        // 下划线
        private void FontUnderline()
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;
            StyleInfo oldstyle = iss.GetCompositeInfo(r, c, 0, null);

            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].SelectionModel[0];

            for (int i = 0; i < cr.RowCount; i++)
            {
                for (int j = 0; j < cr.ColumnCount; j++)
                {
                    StyleInfo newstyle = iss.GetCompositeInfo(i + cr.Row, j + cr.Column, 0, null);
                    newstyle.Font.Underline = !oldstyle.Font.Underline;
                    iss.SetDirectInfo(i + cr.Row, j + cr.Column, newstyle);
                }
            }
        }

        // 左对齐
        private void AlignLeft()
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;

            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].SelectionModel[0];

            for (int i = 0; i < cr.RowCount; i++)
            {
                for (int j = 0; j < cr.ColumnCount; j++)
                {
                    StyleInfo newstyle = iss.GetCompositeInfo(i + cr.Row, j + cr.Column, 0, null);
                    newstyle.HorizontalAlign = HorizontalAlign.Left;
                    iss.SetDirectInfo(i + cr.Row, j + cr.Column, newstyle);
                }
            }
        }

        // 居中对齐
        private void AlignCenter()
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;

            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].SelectionModel[0];

            for (int i = 0; i < cr.RowCount; i++)
            {
                for (int j = 0; j < cr.ColumnCount; j++)
                {
                    StyleInfo newstyle = iss.GetCompositeInfo(i + cr.Row, j + cr.Column, 0, null);
                    newstyle.HorizontalAlign = HorizontalAlign.Center;
                    iss.SetDirectInfo(i + cr.Row, j + cr.Column, newstyle);
                }
            }
        }

        // 右对齐
        private void AlignRight()
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;

            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].SelectionModel[0];

            for (int i = 0; i < cr.RowCount; i++)
            {
                for (int j = 0; j < cr.ColumnCount; j++)
                {
                    StyleInfo newstyle = iss.GetCompositeInfo(i + cr.Row, j + cr.Column, 0, null);
                    newstyle.HorizontalAlign = HorizontalAlign.Right;
                    iss.SetDirectInfo(i + cr.Row, j + cr.Column, newstyle);
                }
            }
        }

        // 合并单元格
        private void MergeCell()
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;
            StyleInfo oldstyle = iss.GetCompositeInfo(r, c, 0, null);

            FarPoint.Web.Spread.Model.CellRange selection = fpTemplate.Sheets[0].SelectionModel[0];
            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].GetSpanCell(r, c);

            if ((cr.RowCount > 1 || cr.ColumnCount > 1))
            {
                fpTemplate.Sheets[0].RemoveSpanCell(r, c);
            }
            else
            {
                fpTemplate.Sheets[0].AddSpanCell(selection.Row, selection.Column, selection.RowCount, selection.ColumnCount);
            }
        }

        // 插入行
        private void InsertRow()
        {
            fpTemplate.Sheets[0].Rows.Add(fpTemplate.Sheets[0].ActiveRow + 1, 1);
        }

        // 删除行
        private void DeleteRow()
        {
            fpTemplate.Sheets[0].Rows.Remove(fpTemplate.Sheets[0].ActiveRow, 1);
        }

        // 行高
        private void RowHeight(int size)
        {
            fpTemplate.Sheets[0].Rows[fpTemplate.Sheets[0].ActiveRow].Height = size;
        }

        // 插入列
        private void InsertColumn()
        {
            fpTemplate.Sheets[0].Columns.Add(fpTemplate.Sheets[0].ActiveColumn, 1);
        }

        // 删除列
        private void DeleteColumn()
        {
            fpTemplate.Sheets[0].Columns.Remove(fpTemplate.Sheets[0].ActiveColumn, 1);
        }

        // 列宽
        private void ColWidth(int size)
        {
            fpTemplate.Sheets[0].Columns[fpTemplate.Sheets[0].ActiveColumn].Width = size;
        }

        // 字体
        private void FontName(string name)
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;
            StyleInfo oldstyle = iss.GetCompositeInfo(r, c, 0, null);

            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].SelectionModel[0];

            for (int i = 0; i < cr.RowCount; i++)
            {
                for (int j = 0; j < cr.ColumnCount; j++)
                {
                    StyleInfo newstyle = iss.GetCompositeInfo(i + cr.Row, j + cr.Column, 0, null);
                    newstyle.Font.Name = oldstyle.Font.Name;
                    iss.SetDirectInfo(i + cr.Row, j + cr.Column, newstyle);
                }
            }
        }

        // 字号
        private void FontSize(int size)
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;
            StyleInfo oldstyle = iss.GetCompositeInfo(r, c, 0, null);

            int s1 = size;
            switch (s1)
            {
                case 1:
                    oldstyle.Font.Size = FontUnit.Smaller;
                    break;
                case 2:
                    oldstyle.Font.Size = FontUnit.Small;
                    break;
                case 3:
                    oldstyle.Font.Size = FontUnit.Medium;
                    break;
                case 4:
                    oldstyle.Font.Size = FontUnit.Larger;
                    break;
                case 5:
                    oldstyle.Font.Size = FontUnit.Large;
                    break;
                case 6:
                    oldstyle.Font.Size = FontUnit.XLarge;
                    break;
            }

            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].SelectionModel[0];

            for (int i = 0; i < cr.RowCount; i++)
            {
                for (int j = 0; j < cr.ColumnCount; j++)
                {
                    StyleInfo newstyle = iss.GetCompositeInfo(i + cr.Row, j + cr.Column, 0, null);
                    newstyle.Font.Size = oldstyle.Font.Size;
                    iss.SetDirectInfo(i + cr.Row, j + cr.Column, newstyle);
                }
            }
        }

        // 背景色
        private void BackColor(Color backcolor)
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;

            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].SelectionModel[0];

            for (int i = 0; i < cr.RowCount; i++)
            {
                for (int j = 0; j < cr.ColumnCount; j++)
                {
                    StyleInfo newstyle = iss.GetCompositeInfo(i + cr.Row, j + cr.Column, 0, null);
                    newstyle.BackColor = backcolor;
                    iss.SetDirectInfo(i + cr.Row, j + cr.Column, newstyle);
                }
            }
        }

        // 前景色
        private void ForeColor(Color forecolor)
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;

            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].SelectionModel[0];

            for (int i = 0; i < cr.RowCount; i++)
            {
                for (int j = 0; j < cr.ColumnCount; j++)
                {
                    StyleInfo newstyle = iss.GetCompositeInfo(i + cr.Row, j + cr.Column, 0, null);
                    newstyle.ForeColor = forecolor;
                    iss.SetDirectInfo(i + cr.Row, j + cr.Column, newstyle);
                }
            }
        }

        // 锁定单元格颜色
        private void LockColor(string name)
        {
            if (name == "常规显示")
            {
                fpTemplate.Sheets[0].LockBackColor = Color.Transparent;
            }
            else
            {
                fpTemplate.Sheets[0].LockBackColor = Color.LightGray;
            }
        }

        // 模板保护模式
        private void SetProtect(string name)
        {
            if (name == "取消保护")
            {
                fpTemplate.Sheets[0].Protect = false;
            }
            else
            {
                fpTemplate.Sheets[0].Protect = true;
            }
        }

        private void UpdateRow(string parameters)
        {

        }

        // 设置单元格绑定数据字段
        private void SetDataField(string name)
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            string newname = name;
            string oldname = "";

            List<string> columns = DataTools.GetTemplateDataFields(hfTemplateID.Value);

            DefaultSheetDataModel dtModel = (fpTemplate.Sheets[0].DataModel as DefaultSheetDataModel);

            var customnames = dtModel.GetCustomNameEnumerator();

            foreach (string col in columns)
            {
                ExternalCellExpression expr = dtModel.GetCustomName(col) as ExternalCellExpression;
                if (expr != null)
                {
                    if (expr.Row == r && expr.Column == c)
                    {
                        oldname = col;
                        fpTemplate.Sheets[0].Cells[expr.Row, expr.Column].ResetText();
                    }
                    if (newname.Equals(col))
                    {
                        fpTemplate.Sheets[0].Cells[expr.Row, expr.Column].ResetText();
                    }
                }
            }

            dtModel.RemoveCustomName(oldname);
            dtModel.RemoveCustomName(newname);

            dtModel.AddCustomName(newname, new ExternalCellExpression(dtModel, r, c));
            fpTemplate.Sheets[0].SetText(r, c, "[" + newname + "]");
        }


        // 删除当前单元格绑定的数据字段
        private void DeleteDataField()
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            DefaultSheetDataModel dtModel = (fpTemplate.Sheets[0].DataModel as DefaultSheetDataModel);

            var customnames = dtModel.GetCustomNameEnumerator();

            while (customnames.MoveNext())
            {
                ExternalCellExpression expr = dtModel.GetCustomName(customnames.Current.ToString()) as ExternalCellExpression;
                if (expr != null)
                {
                    if (expr.Row == r && expr.Column == c)
                    {
                        dtModel.RemoveCustomName(customnames.Current.ToString());
                        fpTemplate.Sheets[0].SetText(r, c, null);
                        break;
                    }
                }
            }
        }

        // 设置单元格的只读属性
        private void CellLock()
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            int c = fpTemplate.Sheets[0].ActiveColumn;

            FarPoint.Web.Spread.Model.ISheetStyleModel iss;
            iss = (FarPoint.Web.Spread.Model.ISheetStyleModel)fpTemplate.Sheets[0].StyleModel;

            StyleInfo newstyle = iss.GetCompositeInfo(r, c, 0, null);

            FarPoint.Web.Spread.Model.CellRange cr = fpTemplate.Sheets[0].SelectionModel[0];

            for (int i = 0; i < cr.RowCount; i++)
            {
                for (int j = 0; j < cr.ColumnCount; j++)
                {
                    fpTemplate.Sheets[0].Cells[r+i, c+j].Locked = !newstyle.Locked;
                }
            }
        }

        // 删除动态行表单中设置的数据行
        private void DeleteDataArea()
        {
            DefaultSheetDataModel dtModel = (fpTemplate.Sheets[0].DataModel as DefaultSheetDataModel);
            dtModel.RemoveCustomName("DataArea");

            // 删除数据行的同时，需要清空全部数据单元格
            ClearDataFields();
        }

        // 设置动态行表单中的数据行
        private void SetDataArea()
        {
            int r = fpTemplate.Sheets[0].ActiveRow;
            DefaultSheetDataModel dtModel = (fpTemplate.Sheets[0].DataModel as DefaultSheetDataModel);

            ExternalRangeExpression expr = dtModel.GetCustomName("DataArea") as ExternalRangeExpression;
            if (expr != null)
            {
                DeleteDataArea();
            }

            dtModel.RemoveCustomName("DataArea");
            dtModel.AddCustomName("DataArea", new ExternalRangeExpression(dtModel, r, -1, 1, 0));
        }

        // 清空全部数据单元格
        private void ClearDataFields()
        {
            List<string> columns = DataTools.GetTemplateDataFields(hfTemplateID.Value);
            DefaultSheetDataModel dtModel = (fpTemplate.Sheets[0].DataModel as DefaultSheetDataModel);

            foreach (string col in columns)
            {
                ExternalCellExpression expr = dtModel.GetCustomName(col) as ExternalCellExpression;
                if (expr != null)
                {
                    dtModel.RemoveCustomName(col);
                    fpTemplate.Sheets[0].SetText(expr.Row, expr.Column, null);
                }
            }
        }

        #endregion

        #region Spread操作

        protected void fpTemplate_ButtonCommand(object sender, SpreadCommandEventArgs e)
        {
            if (e.CommandName.Equals("SaveTemplate"))
                this.SaveTemplate();
            else if (e.CommandName.Equals("FontBold"))
                this.FontBold();
            else if (e.CommandName.Equals("FontItalic"))
                this.FontItalic();
            else if (e.CommandName.Equals("FontUnderline"))
                this.FontUnderline();
            else if (e.CommandName.Equals("MergeCell"))
                this.MergeCell();
            else if (e.CommandName.Equals("InsertRow"))
                this.InsertRow();
            else if (e.CommandName.Equals("DeleteRow"))
                this.DeleteRow();
            else if (e.CommandName.Equals("InsertColumn"))
                this.InsertColumn();
            else if (e.CommandName.Equals("DeleteColumn"))
                this.DeleteColumn();
            else if (e.CommandName.Equals("AlignLeft"))
                this.AlignLeft();
            else if (e.CommandName.Equals("AlignCenter"))
                this.AlignCenter();
            else if (e.CommandName.Equals("AlignRight"))
                this.AlignRight();
            else if (e.CommandName.Equals("SetDataArea"))
                this.SetDataArea();
            else if (e.CommandName.Equals("DeleteDataArea"))
                this.DeleteDataArea();
            else if (e.CommandName.Equals("DeleteDataField"))
                this.DeleteDataField();
            else if (e.CommandName.Equals("CellLock"))
                this.CellLock();
            else if (e.CommandName.StartsWith("DataField."))
            {
                int n = e.CommandName.IndexOf(".") + 1;
                this.SetDataField(e.CommandName.Substring(n));
            }
            else if (e.CommandName.StartsWith("ColWidth"))
            {
                int n = e.CommandName.IndexOf("-") + 1;
                int size = int.Parse(e.CommandName.Substring(n));
                this.ColWidth(size);
            }
            else if (e.CommandName.StartsWith("RowHeight"))
            {
                int n = e.CommandName.IndexOf("-") + 1;
                int size = int.Parse(e.CommandName.Substring(n));
                this.RowHeight(size);
            }
            else if (e.CommandName.StartsWith("FontName"))
            {
                int n = e.CommandName.IndexOf(".") + 1;
                this.FontName(e.CommandName.Substring(n));
            }
            else if (e.CommandName.StartsWith("FontSize"))
            {
                int n = e.CommandName.IndexOf(".") + 1;
                int size = int.Parse(e.CommandName.Substring(n));
                this.FontSize(size);
            }
            else if (e.CommandName.StartsWith("BackColor"))
            {
                int n = e.CommandName.IndexOf(".") + 1;
                Color bc = Color.FromName(e.CommandName.Substring(n));
                this.BackColor(bc);
            }
            else if (e.CommandName.StartsWith("ForeColor"))
            {
                int n = e.CommandName.IndexOf(".") + 1;
                Color fc = Color.FromName(e.CommandName.Substring(n));
                this.ForeColor(fc);
            }
            else if (e.CommandName.StartsWith("LockColor"))
            {
                int n = e.CommandName.IndexOf(".") + 1;
                string name = e.CommandName.Substring(n);
                this.LockColor(name);
            }
            else if (e.CommandName.StartsWith("Protect"))
            {
                int n = e.CommandName.IndexOf(".") + 1;
                string name = e.CommandName.Substring(n);
                this.SetProtect(name);
            }
        }

        #endregion
    }
}